package com.example.materialmotiondemo.ui

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.materialmotiondemo.R
import com.example.materialmotiondemo.databinding.ItemLayoutBinding

class ItemsAdapter(
    private val items: List<String>,
    private val selectionHandler: (View, TextView, Int) -> Unit
) : RecyclerView.Adapter<ItemsAdapter.ItemViewHolder>() {

    inner class ItemViewHolder(
        private val binding: ItemLayoutBinding,
        private val selectionHandler: (View, TextView, Int) -> Unit
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(text: String, position: Int) {
            val context = itemView.context
            binding.card.transitionName = context.getString(R.string.container_item_card, position)
            binding.textView.transitionName =
                context.getString(R.string.shared_elements_item_text, position)
            binding.card.setOnClickListener { card ->
                selectionHandler(card, binding.textView, position)
            }
            binding.textView.text = text
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        ItemViewHolder(
            ItemLayoutBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            ), selectionHandler
        )


    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        holder.bind(items[position], position)
    }

    override fun getItemCount() = items.size
}